package com.dq.controller.goods.stock;

import com.dq.controller.in.goods.stock.OrgStockCheckMainQuery;
import com.dq.controller.in.goods.stock.OrgStockCheckQuery;
import com.dq.domain.goods.stock.StockChangeRecord;
import com.dq.domain.record.stockcheck.StockCheckMain;
import com.dq.domain.record.stockcheck.StockCheckRecord;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */
@RestController
@RequestMapping("/sys/goods/stockCheck/")
@Api(description = "库存盘点管理")
public class SysStockCheckController extends AllService {

    @PostMapping("listMain")
    @ApiOperation(value = "库存盘点main列表", hidden = true)
    public Vo listMain(@RequestBody OrgStockCheckMainQuery orgStockCheckMainQuery) throws AdminException {
        orgStockCheckMainQuery.format();

        List<StockCheckMain> list = stockCheckMainService.list(orgStockCheckMainQuery);

        return new ListDataVo((List<StockCheckMain.Vo>) VO.toVOCollection(list), stockCheckMainService.count(orgStockCheckMainQuery));
    }
    @PostMapping("listInfoAndStock")
    @ApiOperation(value = "库存盘点详情列表", hidden = true)
    public Vo listInfoAndStock(@RequestBody OrgStockCheckQuery orgStockCheckQuery) throws AdminException {
        orgStockCheckQuery.format();
        List<StockCheckRecord> list = stockCheckRecordService.list(orgStockCheckQuery);

        List<StockCheckRecord.Vo> vos = new ArrayList<>();
        for (StockCheckRecord stockCheckRecord : list) {
            StockCheckRecord.Vo vo = (StockCheckRecord.Vo) stockCheckRecord.toVO();
            Long stock = goodsService.productStock(stockCheckRecord.getGoods().getId(), vo.getOrgId());
            vo.setRealstock(stock);
            vos.add(vo);
        }

        return new ListDataVo(vos, stockCheckRecordService.count(orgStockCheckQuery));
    }

    /**
     * 同时删除info
     */
    @GetMapping("delete/{id}")
    @ApiOperation("删除")
    public Vo delete(@ApiParam("id") @PathVariable Long id)throws AdminException{
        stockCheckMainService.delete(id);
        return new Vo(HttpStatus.OK);
    }


    @PostMapping("check")
    @ApiOperation("执行盘点")
    public Vo check(@RequestBody OrgStockCheckMainQuery orgStockCheckMainQuery)throws AdminException{

        stockCheckMainService.check(orgStockCheckMainQuery.getOrgIds());

        return new Vo(HttpStatus.OK);
    }

}
